<script type="text/javascript"> 
$(function(){
    	//操作数量
    var cult = function(){
        var sum=0;
        
        $('.tr_data').each(function(index,item){
            if($(".ckorder",item).attr("checked") == "checked"){
                var price = parseFloat($('.s2 em',item).text().replace(/,/,''));
                var quantity = parseFloat($('.quantity',item).val());
                var shipFee = parseFloat($('.s5',item).text());
                if(isNaN(shipFee))
                    shipFee = 0;

                $('.s4',item).text(changeTwoDecimal_f(price*quantity));


                sum += price *quantity + shipFee;
            }
            
        });
        $('.Statistics_r span').text(changeTwoDecimal_f(sum));
        $(".jiesuan em").text(changeTwoDecimal_f(sum));
    }
    var changeitem =function(tr){
        var selected = $(':checked',tr).length>0?1:0;
        var quantity = $('.quantity',tr).val();
        var comment = $('.Remarks span',tr).text();
        var itemid = $(tr).attr('data');
        $.post('changeitem', {'itemid':itemid,'quantity':quantity,'selected':selected,'comment':comment}, function(){
            return true;
        }, 'json');
    }
	$('.g_jia').click(function(){
		$(this).parent().find(".quantity").val(parseFloat($(this).parent().find(".quantity").val())+1);
        $('.quantity').change();
		return false;
	});
	$('.g_jian').click(function(){
		var quantity= parseFloat($(this).parent().find(".quantity").val())-1;
		quantity = quantity==0 || isNaN(quantity) ?1:quantity;
		$(this).parent().find('.quantity').val(quantity);
        $('.quantity').change();
		return false;
	});
    $('.quantity').change(function(){
        var quantity = parseFloat($(this).val());
        quantity = quantity==0 || isNaN(quantity) ?1:quantity;
        $(this).val(quantity);
        changeitem($(this).parents('tr'));
        cult();
    });
    $('.Remarks .editCommon').click(function(){
        var parent = $(this).parents('tr');
        $('.Remarks2',parent).show();
        $('.Remarks',parent).hide();
        return false;
    });
    $('.Remarks2 .qd').click(function(){
        var parent = $(this).parents('tr');
        $('.Remarks span',parent).text($('.txtcomment',parent).val());
        $('.Remarks2',parent).hide();
        $('.Remarks',parent).show();
        changeitem(parent);
        return false;
    });
    $('input[name="isSelected"]').change(function(){
        changeitem($(this).parents('tr'));
        cult();
    });
    $(".ckorder").click(function(){
        changeitem($(this).parents('tr'));
        cult();
    });
    $('input[name="changgeAll"]').click(function(){
        $('.ckorder').each(function() {
            if($('input[name="changgeAll"]').attr("checked")){
                $(this).attr("checked",true);
            }else{            
                $(this).removeAttr("checked");
            }
            changeitem($(this).parents('tr'));
            cult();
        });
    });
    function changeTwoDecimal_f(x)
    {   
        var f_x = parseFloat(x);
        if (isNaN(f_x))
        {
            alert('function:changeTwoDecimal->parameter error'+x);
            return false;
        }
        
        f_x = Math.round(f_x*100)/100;
        var s_x = f_x.toString();
        var pos_decimal = s_x.indexOf('.');
        if (pos_decimal < 0)
        {
            pos_decimal = s_x.length;
            s_x += '.';
        }
        while (s_x.length <= pos_decimal + 2)
        {
            s_x += '0';
        }
        return s_x;
    }
});
</script>
<div class="content">
	<div class="cart">
        <form action="/purchase/checkout" method="get">
    	<div class="cart_s png_bg">
        	<div class="cart_top">
            	<div class="cart_tz">
                	<h1><em></em><span>我的购物车</span></h1>
                    <p>购物车状态<span>(<?php echo count($this->cart->Items) . '/' . $this->cart->MaxItemsCount ?>)</span></p>
                </div>
                <div class="cart_ty">
                	<div class="tishi">淘宝商品可享受所有卖家促销方式，如：vip折扣、限时促销、店铺vip折扣等！</div>
                    <?php if (count($this->cart->Items)){ ?>
                    <table class="jiesuan">
                    	<tr>
                            <td><p>您应支付：<em>￥<?php echo number_format($this->cart->Total,2); ?></em></p></td>
                            <td class="js"> <input name="" type="submit" value="去结算" onmouseover="this.className='After'" onmouseout="this.className=''" /></td>
                        </tr>
                    </table>
                     <?php }?>
                </div>
            </div>
            <div class="biao_head">
                <ul>
                    <li class="b1"></li>
                    <li class="b2">我挑选的宝贝</li>
                    <li class="b3">单价(元)</li>
                    <li class="b4">数量</li>
                    <li class="b5">小计(元)</li>
                    <li class="b6">运费</li>
                    <li class="b7">操作</li>
                </ul>
            </div>
            <?php 
            if (count($this->cart->Items) == 0){
            ?>
            <div class="kong">
                <h2>您的购物车还是空的，赶紧行动吧！您可以：</h2>
                <ul>
                    <li>如果您还未登录，可能导致购物车为空，请<a href="/user/login">马上登录</a></li>
                    <li>您也可以马上去<a href="/purchase/agent">挑选宝贝</a></li>
                </ul>
            </div>
            <?php
            }  else {
            ?>
            <div class="list">
                <?php foreach ($this->cart->getShops(false) as $shop) {?>
                <table>
                  <tr>
                    <th colspan="6">
                  <div class="Shop">&nbsp;<span style="margin-left: 8px;">&nbsp;
                      <?php echo $shop->IsTaobao?'店铺：':''; ?><a href="<?php echo $shop->Url; ?>" target="_blank"><?php echo $shop->Name ; ?></a></span></div>              
                    </th>
                  </tr>
                  <?php 
                  $isShopFrist=TRUE;
                  $shopItems = $this->cart->getItemsByShopId($shop->Id);
                  foreach ($shopItems as $item){?>
                  <tr class="tr_data" data="<?= $item->Id ?>">
                    <td class="s1">
                        <div class="xz" style="padding:1px;margin-left: 8px;"><span><input type="checkbox" name="myshop" value="<?= $item->Id ?>" <?php echo $item->IsSelected?"checked":"";?> class="ckorder"/></span></div>
                        <div class="sp_pic">
                            <a href="<?php echo $item->Product->Url ?>" target="_blank"><img src="<?php echo $item->Product->RemoteImage ?>" alt="<?php echo $item->ProductName ?>" /></a>                            
                        </div>                        
                        <div class="sp_info">
                            <h1><a href="<?php echo $item->Product->Url ?>" target="_blank"><?php echo $item->ProductName ?></a></h1>
                            <div class="zdy" style="overflow: hidden" title="<?= $item->Product->PropertyAlias ?>">
                                  <?= $item->Product->PropertyAlias ?>
                            </div>
                            <div class="Remarks">
                            	
                                <p>商品备注: <span><?php echo $item->Comment == NULL || $item->Comment == ''?'(无)':$item->Comment ?></span></p><a class="editCommon" href="#">修改</a>
                            </div>
                            <div class="Remarks2" style="display:none">
                            	<label>备注:</label><input class="beizhu bz_hui txtcomment" name="comm" type="text" value="<?php echo $item->Comment == NULL || $item->Comment == ''?'告诉卖家您的特殊要求':$item->Comment ?>" /><input class="qd" name="" type="button" value="确定" />
                            </div>                            
                        </div>
                    </td>
                    <td class="s2">
                        <em><?php echo number_format($item->Price,2); ?></em>
                    </td>
                    <td class="s3">
                        <div class="gwc_num"><a class="g_jian" href="#" title="减">减</a><input name="quantity" class="quantity" type="text" value="<?php echo $item->Quantity; ?>" /><a class="g_jia" href="#" title="加">加</a></div>
                        
                    </td>
                    <td class="s4"><?php echo number_format($item->SubTotal,2) ?></td>
                    <?php if($isShopFrist){ ?>
                    <td class="s5" rowspan="<?php echo count($shopItems) ?>"><?php echo $item->ShipFee; ?></td>
                    <?php $isShopFrist = FALSE;}?>
                    <td class="s6">
                        <p><a href="/purchase/delete/?id=<?php echo $item->Id ?>">删除</a></p>
                    </td>
                  </tr>
                  <?php }?>
                </table>
                <?php }?>
                <div style="margin-bottom: 8px;">&nbsp;
                    <span><input type="checkbox" name="changgeAll"></span>　
                    <span><a href="javascript:dellist();" color="blue">删除选中</a></span>
                </div>
            </div>
            <div class="Statistics">
            	<div class="Statistics_l"></div>
                <div class="Statistics_r">
                	<dl>
                        <dd>您应支付：<span><?php echo number_format($this->cart->Total,2) ?></span></dd>
                    </dl>
                </div>
            </div>
            <div class="Operating">
            	<div class="Operating_l">
                	
                </div>
                <div class="Operating_r">
                	<dl>
                        <dt><a href="/purchase/agent"><input name="" type="button" onmouseover="this.className='After'" onmouseout="this.className=''" /></a></dt>
                        <dd><input id="submit_button" name="submit_button" value="" type="submit" onmouseover="this.className='After'" onmouseout="this.className=''" /></dd>
                    </dl>
                </div>
            </div>
            <?php
            }
            ?>
        </div>
        </form>
    </div> 
</div>

<script type="text/javascript">
       
    function dellist(){
        var idList = "";
        $('input[name="myshop"]:checked').each(function(){
           idList += $(this).val() + ",";
        });
        if(idList){
            $.post(
                '/purchase/delList',
                {'idList':idList},
                function (){
                    window.location.href = "/purchase/cart";
                }
            );
        }        
    }
    $("#submit_button").click(function(){
        var idList = "";
        $('input[name="myshop"]:checked').each(function(){
           idList += $(this).val() + ",";
        });
        if(idList == ""){
            alert("请选择您所需提交的商品");
            return false;
        }
    });
</script>